import bisect
class Solution(object):
    def kIncreasing(self, arr, k):
        def dfs(s):
            st = []
            for num in s:
                index = bisect.bisect_right(st, num)
                if index == len(st):
                    st.append(num)
                else:
                    st[index] = num
            return len(s) - len(st) 
        ans = 0
        for i in range(k):
            s = arr[i::k]
            ans += dfs(s)
        return ans
            

data = Solution()
arr = [4,1,5,2,6,2]
k = 3
print(data.kIncreasing(arr, k))